package alo.p3.work;

import alo.p3.ListNode;
//循环链表，删除第m个结点，重复至只剩一个结点
public class JosephusPosition {

    public static void main(String[] args) {
        JosephusPosition j=new JosephusPosition();
        j.JosephusPosition(10,3);
    }


    void JosephusPosition(int N,int M){
        ListNode p =new ListNode(1),q;

        //建立一个数字的循环列表
        q=p;
        for (int i=2;i<=N;i++){
            p.setNext(new ListNode(i));
            p=p.getNext();
        }
        p.setNext(q);


        //淘汰第M个选手
        for (int count=N;count>1;count--){
            for (int i=0;i<M-1;i++)
                p=p.getNext();
            p.setNext(p.getNext().getNext());
        }
        System.out.println("Last plyer left stangding(Joesphus Position) is "+p.getData());
    }
}
